package com.android1608.administrator.rsa;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.TextView;

import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

//非对称加密
public class MainActivity extends AppCompatActivity {

    private PrivateKey mPrivateKey;
    private PublicKey mPublicKey;

    private TextView showKeys;
    private String mString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        showKeys = (TextView) findViewById(R.id.showKeys);
    }

    public void generate(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //1.获取生成的对象
                try {
                    KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
                    //2.初始化
                    generator.initialize(1024);
                    //3.获取公钥私钥
                    KeyPair keyPair = generator.generateKeyPair();
                    //3.1获取私钥
                    mPrivateKey = keyPair.getPrivate();
                    //3.2获取公钥
                    mPublicKey = keyPair.getPublic();
                    //私钥具体数据
                    byte[] mPrivateKeyEncoded = mPrivateKey.getEncoded();
                    //
                    byte[] mPublicKeyEncoded = mPublicKey.getEncoded();
                    //进行Base64编码
                    final byte[] privateKeyBase64Encoded = Base64.encode(mPrivateKeyEncoded, Base64.DEFAULT);
                    final byte[] publicKeyBase64Encoded = Base64.encode(mPublicKeyEncoded, Base64.DEFAULT);

                    //显示到文本框中
                    showKeys.post(new Runnable() {
                        @Override
                        public void run() {
                            showKeys.setText("私钥是："+"\n"+new String(privateKeyBase64Encoded)+"\n"
                                    +"公钥是："+"\n"+new String(publicKeyBase64Encoded));
                        }
                    });

                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

    //使用公钥私钥进行加密解密
    public void encrypt(View view) {
        //使用私钥加密，公钥解密
        String check = "999999999,123654";
        switch (view.getId()){
            case R.id.rasEncrypt:
                try {
                    //1.获取加密引擎
                    Cipher cipher = Cipher.getInstance("RSA");
                    //2.初始化
                    if (mPrivateKey != null) {
                        cipher.init(Cipher.ENCRYPT_MODE,mPrivateKey);
                    }
                    //3.加密
                    byte[] doFinal = cipher.doFinal(check.getBytes());
                    byte[] base64Encode = Base64.encode(doFinal, Base64.DEFAULT);

                    mString = new String(base64Encode);
                    showKeys.setText("RSA加密的数据是："+ mString);

                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.rasDecrypt:
                try {
                    //1.获取解密引擎
                    Cipher cipher = Cipher.getInstance("RSA");
                    //2.初始化
                    if (mPublicKey != null) {
                        cipher.init(Cipher.DECRYPT_MODE,mPublicKey);
                    }
                    //3.解密
                    if (mString != null) {

                        byte[] base64Encode = Base64.decode(mString, Base64.DEFAULT);

                        byte[] doFinal = cipher.doFinal(base64Encode);

                        showKeys.setText("RSA解密的数据是："+new String(doFinal));
                    }

                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
